package com.vm.action.system;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.JsonUtile;
import com.vm.dao.system.IRole;
import com.vm.entity.Role;
import com.vm.entity.User;
import com.vm.log4j.CustomizeLog4j;

/**
 * @time 2011-08-08
 * @author osnt
 * **/
public class RoleAction extends ActionSuper implements Preparable {

	private static Logger _log = CustomizeLog4j.getLogger(RoleAction.class);
	private Role role;
	private IRole roleDAO;
	private List<Role> listRole;
	private String jsonTree;
	private String[] roleGrpValues;
	private String jsonButtens;
	private String jsonMenuKey;
	private String menuGrpValues;
	private String menuButValues;
	private Map<String,String> signAddCdMap;
	public void prepare() throws Exception {
		super.setButtonGrpMap(roleDAO.seletButtonGrp((User)this.getSession().get("user"), "roleAction"));
		signAddCdMap = new HashMap<String, String>();
//		signAddCdMap.put("1", "管理系统");
//		signAddCdMap.put("0", "海图");
	}

	public String list() throws Exception {
		listRole = roleDAO.findAll();
		return SUCCESS;
	}

	public String assignPrmss() {
		role = roleDAO.findById(role.getRlId());
		jsonButtens = JsonUtile.mapToJson(roleDAO.findallButtens());
		List<Map<String, Object>> listmap = roleDAO.findAllMenu();
		String[] ids = roleDAO.getGrpValues(role.getPrmssGrp());
		for(Map<String,Object> map : listmap){
			String id = map.get("value").toString();
			for(int j=0;j<ids.length;j++){
				if(id.equals(ids[j])){
					map.put("checked", true);
				}
			}
		}
		jsonTree = JsonUtile.mapToJson(listmap);
		String hql = "select  menuId,prmssVal FROM MenuButton where rlId='"+role.getRlId()+"'";
		jsonMenuKey = JsonUtile.getJsonMap(roleDAO.getCheckBoxMap(hql));
		return SUCCESS;
	}

	public String add() throws Exception {
		return SUCCESS;
	}

	public String save() throws Exception {
		String[] msgArg = new String[] { "roleAction_list.do" };
		String msg = null;
		try {
			role.setModulPrmssCd(roleDAO.getGrp(role.getClass().getName(),"modulPrmssCd"));
			roleDAO.save(role);
			msg = super.getResourceMessage("insert_right_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("insert_right_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	public String edit() throws Exception {
		role = roleDAO.findById(role.getRlId());
		return SUCCESS;
	}
	
	public String view() throws Exception {
		role = roleDAO.findById(role.getRlId());
		return SUCCESS;
	}
	public String update() throws Exception {
		String[] msgArg = new String[] { "roleAction_list.do" };
		String msg = null;
		try {
			roleDAO.update(role);
			msg = super.getResourceMessage("update_role_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("update_role_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	public String updateGrp() throws Exception {
		String[] msgArg = new String[] { "roleAction_list.do" };
		String msg = null;
		try {
			Map<String,String> btnMap = new HashMap<String,String>();
			ObjectMapper mapper = new ObjectMapper();
			btnMap = mapper.readValue(menuButValues, Map.class);
			roleDAO.updateGrp(role.getRlId(), btnMap, menuGrpValues);
			msg = super.getResourceMessage("update_roleGrp_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("update_roleGrp_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	public String delete() throws Exception {
		String[] msgArg = new String[] { "roleAction_list.do" };
		String msg = null;
		try {
			roleDAO.deleteById(role.getRlId());
			msg = super.getResourceMessage("delete_role_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("delete_role_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public void setRoleDAO(IRole roleDAO) {
		this.roleDAO = roleDAO;
	}

	public IRole getRoleDAO() {
		return roleDAO;
	}

	public List<Role> getListRole() {
		return listRole;
	}

	public void setListRole(List<Role> listRole) {
		this.listRole = listRole;
	}

	public String getJsonTree() {
		return jsonTree;
	}

	public void setJsonTree(String jsonTree) {
		this.jsonTree = jsonTree;
	}

	public String[] getRoleGrpValues() {
		return roleGrpValues;
	}

	public void setRoleGrpValues(String[] roleGrpValues) {
		this.roleGrpValues = roleGrpValues;
	}

	public String getJsonButtens() {
		return jsonButtens;
	}

	public void setJsonButtens(String jsonButtens) {
		this.jsonButtens = jsonButtens;
	}

	public String getJsonMenuKey() {
		return jsonMenuKey;
	}

	public void setJsonMenuKey(String jsonMenuKey) {
		this.jsonMenuKey = jsonMenuKey;
	}

	public String getMenuGrpValues() {
		return menuGrpValues;
	}

	public void setMenuGrpValues(String menuGrpValues) {
		this.menuGrpValues = menuGrpValues;
	}

	public String getMenuButValues() {
		return menuButValues;
	}

	public void setMenuButValues(String menuButValues) {
		this.menuButValues = menuButValues;
	}

	public Map<String, String> getSignAddCdMap() {
		return signAddCdMap;
	}

	public void setSignAddCdMap(Map<String, String> signAddCdMap) {
		this.signAddCdMap = signAddCdMap;
	}
	
}
